/**
 * Created by Administrator on 2016/8/16.
 */
var maxSize;
var currentPage = 0;
var max = $('#all');
var current = $('#current');
var tbody = $('#news_tbody');
var pre = $('#pre');
var next = $('#next');
var orginno_div = $("#orginno_div");
var key_div = $("#key_div");
var msg;
var push_path = "/push/send/all";
var mycategory;
var id;
$(function(){
    init();
    eventinit();
});

function init(){
    getAllClass();
    init_type();
    current.text(currentPage+1);
    $.ajax({
        type:'GET',
        url:host+"/news/number",
        async:false,
        success:function (data) {
            maxSize = data.msg;
            max.text(parseInt(maxSize/news_count));
            page = currentPage+1;
            turnpage(page);
            currentPage++;
            current.text(currentPage);
        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function eventinit(){

    pre.on("click",function(){
        if(currentPage > 1){
            page = currentPage-1;
            turnpage(page);
            currentPage--;
            current.text(currentPage);
        }
    });

    next.on("click",function(){
        if(currentPage < maxSize){
            page = currentPage+1;
            turnpage(page);
            currentPage++;
            current.text(currentPage);
        }
    });
}

//获取某个id下的分类名
function getCategoryById(id) {

        var name = ""
    $.each(JSON.parse(mycategory),function (i, item) {

        if (item.id == id){
            name =  item.name;
            return;
        }
    })
    return name;
}
function turnpage(page){
    $.ajax({
        type:'GET',
        url:host+"/news/all",
        data:{count:news_count,page:page},
        async:false,
        success:function (data) {
            changeTable(data);
        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function changeTable(data){
    tbody.empty();
    $.each(data.list,function(i,item){
        var tr = ' <tr>'+
            ' <td >'+item.title+'</td>'+
            '<td>'+item.author+'</td>'+
            '<td>'+item.introduce+'</td>'+
            ' <td>'+getCategoryById(item.classid)+'</td>'+
            '<td>'+item.addtime+'</td>'+
            '<td><button class="btn btn-primary edit" onclick="turndetail('+item.id+')">详情</button>' +
            '<button class="btn btn-primary edit" onclick="delete_item('+item.id+')">删除</button>'+
            '</td>'+
            ' </tr>';
        tbody.append(tr);
    });
}

//获取所有分类
function getAllClass() {
    $.ajax({
        type: 'GET',
        url: host + "/news/class",
        async: false,
        success: function (data) {

            mycategory = JSON.stringify(data.list);

        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function turndetail(id){
    window.location.replace("detail.html?id="+id);
}


$(".push").on('click',function(){
    ajax_push();
});

function push_action(tempid) {
    id = tempid;
    $.ajax({
        type: 'GET',
        url: host + "/news",
        data: {id: id},
        async: false,
        success: function (data) {
            if (data.code == 1) {
                msg =data;
                $('#myModal').modal('show');
                radio_even();
            } else {
                alert(data.msg);
            }
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}
function delete_item(id) {
    $.ajax({
        type: 'DELETE',
        url: host + "/news?id="+id,
        async: false,
        success: function (data) {
            if (data.code == 1) {
                alert(data.msg);
                window.location.reload();
            } else {
                alert(data.msg);
            }
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}


function radio_even() {
    $("input[name='inlineRadioOptions']").on("click", function () {
        var radio_value = $("input[name='inlineRadioOptions']:checked").val();
        orginno_div.hide();
        key_div.hide();
        switch (radio_value) {
            case "1" :
                setpath("/push/send/all");
                break;
            case "2" :
                setpath("/push/send/terminnals");
                break;
            case "3" :
                setpath("/push/send/users");
                break;
            case "4" :
                orginno_div.show();
                setpath("/push/send/origin/all");
                break;
            case "5" :
                orginno_div.show();
                setpath("/push/send/origin/terminnals");
                break;
            case "6" :
                orginno_div.show();
                setpath("/push/send/origin/users");
                break;
            case "7" :
                key_div.show();
                setpath("/push/send/user");
                break;
            case "8" :
                key_div.show();
                setpath("/push/send/terminnal");
                break;
        }

    });

}

function setpath(path) {
    push_path = path;
}


function ajax_push(){
    var type = $('#type').val();
    msg.type = type;
    msg = JSON.stringify(msg);
    $.ajax({
        type: 'POST',
        url: host + push_path,
        data: {msg: msg, orginno:getCategory(), key: $('#key').val(),type:type},
        success: function (data) {
            $('#myModal').modal('hide');
            alert(data.msg);
        },
        error: function (XMLHttpRequest) {
            $('#myModal').modal('hide');
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });

}


function init_type(){
    var typeObj = $.parseJSON(newsJson);
    var option= "";
    $.each(typeObj,function(i,item){
        var temp = '<option value="'+item.type+'">'+item.value+'</option>';
        option+=temp;
    });
    $('#type').append(option);
}

function change_classid(classid){
    var newsclass = $.parseJSON(classJson);
    var classname = "";
    var ope = "";
    $.each(newsclass,function(i,item){
        if(item.id == classid){
            classname = item.name;
        }
    });
    return classname;

}


